/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.tendon.convert.json.listeners;

import com.tencent.tendon.convert.util.*;
import com.tencent.tendon.convert.json.*;

/**
 *
 * @author nbzhang
 */
public final class JsonByteArrayListener extends JsonAbstractListener<byte[]> {

    private static final JsonByteArrayListener instance = new JsonByteArrayListener();

    private static final JsonByteListener listener = JsonByteListener.getInstance();

    private JsonByteArrayListener() {
    }

    public static JsonByteArrayListener getInstance() {
        return instance;
    }

    @Override
    public byte[] convertFrom(final JsonReader in) {
        int size = 0;
        byte[] data = new byte[8];
        in.checkArray();
        while (in.hasNext()) {
            if (size >= data.length) {
                byte[] newdata = new byte[data.length + 4];
                System.arraycopy(data, 0, newdata, 0, size);
                data = newdata;
            }
            data[size++] = listener.convertByteFrom(in);
        }
        byte[] newdata = new byte[size];
        System.arraycopy(data, 0, newdata, 0, size);
        return newdata;
    }

    @Override
    public final void convertTo(final JsonWriter out, final byte[] value) {
        byte[] values = value;
        if (values == null) {
            out.writeNull();
            return;
        }
        if (values.length == 0) {
            out.write('[', ']');
            return;
        }

        out.write('[');
        boolean first = true;
        for (byte v : values) {
            if (!first) out.write(',');
            listener.convertByteTo(out, v);
            if (first) first = false;
        }
        out.write(']');
    }

    @Override
    public Class<byte[]> getType() {
        return byte[].class;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }

    public static final class JsonByteArrayHexListener extends JsonAbstractListener<byte[]> {

        private static final JsonByteArrayHexListener instance = new JsonByteArrayHexListener();

        private JsonByteArrayHexListener() {
        }

        public static JsonByteArrayHexListener getInstance() {
            return instance;
        }

        @Override
        public void convertTo(JsonWriter out, byte[] value) {
            if (value == null) {
                out.writeNull();
                return;
            }
            out.write('"');
            out.write(Utility.binToHex(value));
            out.write('"');
        }

        @Override
        public byte[] convertFrom(final JsonReader in) {
            char[] text = in.readValue();
            if (text == null) return null;
            return Utility.hexToBin(text);
        }

        @Override
        public Class<byte[]> getType() {
            return byte[].class;
        }
    }
}
